﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NRemedy.Linq;
using BMC.ARSystem;
using System.Linq.Expressions;
using NRemedy.Test.Util;
using System.Reflection;
namespace NRemedy.Test.Linq
{
    [TestClass]
    public class LinqToAR_Real
    {
        /*
         
         * where clause test below
         
         */
        [TestMethod]//where clause with string
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where1()
        {
            //ignore
            using (ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin"))
            {
                ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
                var q = from s in rgf
                        where s.CharacterField == "string"
                        select s;
                List<RegularForm> result = q.ToList();
                RegularForm r = result.First();
                Assert.AreEqual(1, result.Count);
                Assert.AreEqual("string", r.CharacterField);
                Assert.AreEqual(65533, r.IntegerField);
                Assert.AreEqual(2.3, r.RealNumberField);
                Assert.AreEqual(3.3m, r.DecimalNumberField);
                Assert.AreEqual(RegularForm.DropDownListField_Enum.Modify, r.DropDownListField);
                Assert.AreEqual(RegularForm.RadioButtonField_Enum.Default2, r.RadioButtonField);

            }
        }

        [TestMethod]//where clause with int
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where2()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.IntegerField == 65533
                    select s;
            List<RegularForm> result = q.ToList();
            RegularForm r = result.First();
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("string", r.CharacterField);
            Assert.AreEqual(65533, r.IntegerField);
            Assert.AreEqual(2.3, r.RealNumberField);
            Assert.AreEqual(3.3m, r.DecimalNumberField);
            Assert.AreEqual(RegularForm.DropDownListField_Enum.Modify, r.DropDownListField);
            Assert.AreEqual(RegularForm.RadioButtonField_Enum.Default2, r.RadioButtonField);

            context.Dispose();
        }

        [TestMethod]//where clause with real
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where3()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.RealNumberField == 2.3
                    select s;
            List<RegularForm> result = q.ToList();
            RegularForm r = result.First();
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("string", r.CharacterField);
            Assert.AreEqual(65533, r.IntegerField);
            Assert.AreEqual(2.3, r.RealNumberField);
            Assert.AreEqual(3.3m, r.DecimalNumberField);
            Assert.AreEqual(RegularForm.DropDownListField_Enum.Modify, r.DropDownListField);
            Assert.AreEqual(RegularForm.RadioButtonField_Enum.Default2, r.RadioButtonField);

            context.Dispose();
        }

        [TestMethod]//where clause with real
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where4()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.DecimalNumberField == 3.3m
                    select s;
            List<RegularForm> result = q.ToList();
            RegularForm r = result.First();
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("string", r.CharacterField);
            Assert.AreEqual(65533, r.IntegerField);
            Assert.AreEqual(2.3, r.RealNumberField);
            Assert.AreEqual(3.3m, r.DecimalNumberField);
            Assert.AreEqual(RegularForm.DropDownListField_Enum.Modify, r.DropDownListField);
            Assert.AreEqual(RegularForm.RadioButtonField_Enum.Default2, r.RadioButtonField);

            context.Dispose();
        }

        [TestMethod]//where clause with RadioButtonField
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where5()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.RadioButtonField == RegularForm.RadioButtonField_Enum.Default2
                    select s;
            List<RegularForm> result = q.ToList();
            RegularForm r = result.First();
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("string", r.CharacterField);
            Assert.AreEqual(65533, r.IntegerField);
            Assert.AreEqual(2.3, r.RealNumberField);
            Assert.AreEqual(3.3m, r.DecimalNumberField);
            Assert.AreEqual(RegularForm.DropDownListField_Enum.Modify, r.DropDownListField);
            Assert.AreEqual(RegularForm.RadioButtonField_Enum.Default2, r.RadioButtonField);

            context.Dispose();
        }

        [TestMethod]//where clause with DropDownListField
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where6()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.DropDownListField == RegularForm.DropDownListField_Enum.Modify
                    select s;
            List<RegularForm> result = q.ToList();
            RegularForm r = result.First();
            Assert.AreEqual(2, result.Count);
            

            context.Dispose();
        }

        [TestMethod]//where clause with DateTime
        [ExpectedException(typeof(CustomAttributeFormatException))]
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where7()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            DateTime dt = DateTime.Parse("8/16/2012 12:00:00");
            var q = from s in rgf
                    where s.DateTimeField == dt
                    select s;
            List<RegularForm> result = q.ToList();
            RegularForm r = result.First();
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("string", r.CharacterField);
            Assert.AreEqual(65533, r.IntegerField);
            Assert.AreEqual(2.3, r.RealNumberField);
            Assert.AreEqual(3.3m, r.DecimalNumberField);
            Assert.AreEqual(RegularForm.DropDownListField_Enum.Modify, r.DropDownListField);
            Assert.AreEqual(RegularForm.RadioButtonField_Enum.Default2, r.RadioButtonField);

            context.Dispose();
        }

        [TestMethod]//where clause with string && real
        public void _vs31srm01_LinqToAR_Where_Clause_No_Where8()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.CharacterField == "string" && s.RealNumberField == 2.4
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(0, result.Count);
            context.Dispose();
        }


        /*
         * count clause
         * */
        [TestMethod]//where clause with count
        public void _vs31srm01_LinqToAR_Count_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.CharacterField == "string" && s.RealNumberField == 2.4
                    select s;
            Assert.AreEqual(0, q.Count());
            context.Dispose();
        }

        [TestMethod]//where clause with count
        public void _vs31srm01_LinqToAR_Count_Clause2()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    //where s.CharacterField == "Hello World!!!"
                    select s;
            Assert.AreEqual(117, q.Count());
            context.Dispose();
        }

        [TestMethod]//where clause with count
        public void _vs31srm01_LinqToAR_Count_Clause3()
        {
            //ignore
            using (ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin"))
            {
                ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
                var q = from s in rgf
                        where s.Submitter == "Hello World!!!"
                        select s;
                Assert.AreEqual(8, q.Count());
            }
        }


        /*
         * select clause
         * */

        [TestMethod]//select clause
        public void _vs31srm01_LinqToAR_Count_Clause_With_Select_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.Submitter == "Hello World!!!"
                    select s.Submitter;

            var result = q.ToList();
            Assert.AreEqual(8, result.Count);
            foreach (var r in result)
            {
                Assert.AreEqual(true, r == "Hello World!!!");
            }
            context.Dispose();
        }

        [TestMethod]//select clause
        public void _vs31srm01_LinqToAR_Count_Clause_With_Select_Clause2()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.DropDownListField == RegularForm.DropDownListField_Enum.Modify
                    select s.DropDownListField;

            var result = q.ToList();
            Assert.AreEqual(2, result.Count);
            foreach (var r in result)
            {
                Assert.AreEqual(true, r == RegularForm.DropDownListField_Enum.Modify);
            }
            context.Dispose();
        }

        [TestMethod]//select clause
        public void _vs31srm01_LinqToAR_Count_Clause_With_Select_Clause3()
        {
            //ignore
            using (ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin"))
            {
                ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
                var q = from s in rgf
                        where s.Submitter == "Linq"
                        select new
                        {
                            s.Submitter,
                            s.CheckBoxField,
                            s.IntegerField,
                            s.DateField
                        };

                var result = q.ToList();
                Assert.AreEqual(1, result.Count);
                var r = result.First();
                Assert.AreEqual(true, r.Submitter == "Linq");
                Assert.AreEqual(true, r.IntegerField == 2);
                Assert.AreEqual(true, r.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2);
                Assert.AreEqual(true, r.DateField.Month == 8);

            }
        }

        [TestMethod]//select clause
        public void _vs31srm01_LinqToAR_Count_Clause_With_Select_Clause4()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.Submitter == "Linq"
                    select new SelectTarget
                    {
                        str1 = s.Submitter,
                        Int1 = s.IntegerField
                    };

            var result = q.ToList();
            Assert.AreEqual(1, result.Count);
            var r = result.First();
            Assert.AreEqual(true, r.str1 == "Linq");
            Assert.AreEqual(true, r.Int1 == 2);
            context.Dispose();
        }

        [TestMethod]//select clause
        public void _vs31srm01_LinqToAR_Count_Clause_With_Select_Clause5()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.Submitter == "Linq"
                    select new
                    {
                        str1 = s.Submitter,
                        Int1 = s.IntegerField
                    };

            var result = q.ToList();
            Assert.AreEqual(1, result.Count);
            var r = result.First();
            Assert.AreEqual(true, r.str1 == "Linq");
            Assert.AreEqual(true, r.Int1 == 2);
            context.Dispose();
            
        }

        [TestMethod]//select clause
        public void _vs31srm01_LinqToAR_Count_Clause_With_Select_Clause6()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerDefaultFactory());
            var q = from s in rgf
                    where s.Submitter == "Linq"
                    select new RegularForm
                    {
                        Submitter = s.Submitter,
                        RealNumberField = s.RealNumberField,
                        DropDownListField = s.DropDownListField
                    };

            var result = q.ToList();
            Assert.AreEqual(1, result.Count);
            var r = result.First();
            Assert.AreEqual(true, r.Submitter == "Linq");
            Assert.AreEqual(true, r.RealNumberField == 1.1);
            Assert.AreEqual(true, r.IntegerField == 0);
            Assert.AreEqual(true, r.DropDownListField == RegularForm.DropDownListField_Enum.Delete);
            context.Dispose();
        }



        private class SelectTarget
        {
            public string str1 { get; set; }
            public int Int1 { get; set; }
        }
    }
}
